package com.school_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school_admin.config.Result;
import com.school_admin.entity.Category;
import com.school_admin.service.impl.CategoryServiceImpl;
import com.school_admin.service.impl.PostServiceImpl;
import com.school_admin.util.CreateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    private final CategoryServiceImpl categoryService;
    private final CreateTime createTime;
    private final PostServiceImpl postService;

    @Autowired
    public CategoryController(CategoryServiceImpl categoryService, CreateTime createTime, PostServiceImpl postService) {
        this.categoryService = categoryService;
        this.createTime = createTime;
        this.postService = postService;
    }

    // 增删改查
    @PostMapping("create")
    public Result<?> createCategory(Category category) {
        category.setTime(createTime.getTime());
        boolean is = categoryService.save(category);
        if (is){
            return Result.success("创建成功");
        }
        return Result.fail("创建失败");
    }

    @DeleteMapping("delete/{categoryId}")
    public Result<String> deleteCategory(@PathVariable Integer categoryId) {
        boolean is = categoryService.removeById(categoryId);
        if (is) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    // 跟新分类信息
    @PutMapping("update")
    public Result<String> updateCategory(Category category) {
        boolean is = categoryService.updateById(category);
        if (is) {
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    // 查询单个分类
    @GetMapping("find")
    public Result<?> findCategory(Integer categoryId,Integer page,Integer pageSize) {
        return categoryService.findCategory(categoryId,page,pageSize);
    }

    // 获取全部
    @GetMapping("list")
    public Result<?> getCategoryList(Integer type) {
        System.out.println("type = " + type);
        // TODO 1. 查询分类列表
        return categoryService.getCategoryList(type);
    }

    // TODO 分页获取分类(用于后台)
    @GetMapping("listPage")
    public String getCategoryListPage() {
        return "listPage";
    }

    // TODO 获取子分类数据
    @GetMapping("getCategoryParentId")
    public Result<?> getCategoryParentId() {
        //TODO 1. 查询 parentId 为空的数据
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(Category::getParentId);
        //TODO 2. 分页显示
        Page<Category> page = new Page<>(1, 5);
        categoryService.page(page, wrapper);
        categoryService.list(wrapper);
        //TODO 2. 返回数据
        return Result.success(page.getRecords());
    }
    // TODO 修改分类状态
    @PutMapping("changeStatus/{categoryId}/{status}")
    public Result<?> changeCategoryStatus(@PathVariable Integer categoryId,
                                          @PathVariable Integer status) {
        //TODO 1. 更新分类状态
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setStatus(status);
        categoryService.updateById(category);
        //TODO 2. 返回结果
        return Result.success();
    }
    @GetMapping("/{categoryId}")
    public Result<?> getCategoryById(@PathVariable Integer categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }
    @GetMapping("search")
    public Result<?> searchCategory(String categoryName) {
        return categoryService.getSearchCategoryList(categoryName);
    }
}
